﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Price : System.Web.UI.Page
{
    LocalFunctions FUNC;
    int iPriceType = 0;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Admin)this.Master).FUNC;

        btnArticleSave.Text = FUNC.GetPhrase(9);

        lblTextTitle.Text = FUNC.GetPhrase(10);
        lblTextContent.Text = FUNC.GetPhrase(11);

        // Get data from DB about page
        if (!IsPostBack)
        {
            GetData();
           
        }
        ddType_SelectedIndexChanged(this, new EventArgs());
    }

    protected override void OnPreRender(EventArgs e)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(iPriceType);
        Dal.arParams.Add(FUNC.LangID); // Curent language

        DataTable dtPrice = Dal.ExecuteDBsp("Admin_GetPriceByType", Dal.arParams, true);
        dgPriceList.DataSource = dtPrice;
        dgPriceList.DataBind();
    }

    private void GetData()
    {
        //Dal.arParams.Clear();
        //Dal.arParams.Add((int)((Admin)this.Master).CurLang); // Curent language
        DataTable DT = Dal.ExecuteDBsp("Admin_GetTags");

        // If not null - fill fields
        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                ddType.DataSource = DT;
                ddType.DataBind();
            }

        Dal.arParams.Clear();
        Dal.arParams.Add(2); // typeID - 2-pricelist
        DataTable DTTextBlock = Dal.ExecuteDBsp("Admin_GetPageByID", Dal.arParams);

        // If not null - fill fields
        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                DataRow Row = DTTextBlock.Rows[0];
                hfID.Value = Row["ID"].ToString();
                txtMainBlockTitle.Text = Row["Title"].ToString();
                txtContent.Text = Helper.ConvertStringFromSql(Row["Content"].ToString());
            }
        
    }

    protected void btnArticleSave_Click(object sender, EventArgs e)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(Convert.ToInt32(hfID.Value)); //Page ID
        Dal.arParams.Add(txtMainBlockTitle.Text); //Title
        Dal.arParams.Add(Helper.ConvertStringToSql(txtContent.Text)); //Content
        Dal.ExecuteDBsp("Admin_UpdatePageByID", Dal.arParams, true);
    }

    #region Price grid
    protected void ddType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ////Get records by type

        iPriceType = Convert.ToInt32(ddType.SelectedValue);

    }

    protected void dgPriceList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        dgPriceList.EditIndex = e.NewEditIndex;
    }

    protected void dgPriceList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        dgPriceList.EditIndex = -1;
    }

    protected void dgPriceList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = dgPriceList.Rows[e.RowIndex];

        String sName = ((TextBox)row.FindControl("txtPrice")).Text;
        String sContent = ((TextBox)row.FindControl("txtDescription")).Text;

        Int32 iID = Convert.ToInt32(((HiddenField)row.FindControl("hfID")).Value);

        UpdateRecord(iID, sName, sContent);
        dgPriceList.EditIndex = -1;
    }

    private void UpdateRecord(int iID, string sPrice, string sContent)
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(iID);
        Dal.arParams.Add(iPriceType);
        Dal.arParams.Add(Helper.ConvertStringToSql(sPrice));
        Dal.arParams.Add(Helper.ConvertStringToSql(sContent));
        Dal.arParams.Add(FUNC.LangID);

        Dal.ExecuteDBsp("Admin_UpdatePrice", Dal.arParams, true);
    }

    protected void dgPriceList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = dgPriceList.Rows[e.RowIndex];

        Int32 iID = Convert.ToInt32(((HiddenField)row.FindControl("hfID")).Value);
        Dal.arParams.Clear();
        Dal.arParams.Add(iID);
        Dal.ExecuteDBsp("Admin_DeletePrice", Dal.arParams);
    }

    protected void btnFooterSave_Click(object sender, EventArgs e)
    {
        GridViewRow row = dgPriceList.FooterRow;
        String sName=string.Empty;
        String sContent = string.Empty;

        if (row != null)
        {
            sName = ((TextBox)row.FindControl("txtPrice")).Text;
            sContent = ((TextBox)row.FindControl("txtDescription")).Text;
        }
        else
        {
            GridViewRow gvr = (GridViewRow)dgPriceList.Controls[0].Controls[0];
            TableCell Cell = gvr.Cells[0];
            sName = ((TextBox)Cell.FindControl("txtPrice")).Text;
            sContent = ((TextBox)Cell.FindControl("txtDescription")).Text;
        }
       
        UpdateRecord(0, sName, sContent);
    }
    #endregion
}